<?php
$response=array();
require_once('db_config.php');
/* Open a connection */
$mysqli = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_DATABASE);

$query_khuvuc="select id,tenkhuvuc from dmkhuvuc";
if ($stmt=$mysqli->query($query_khuvuc)) 
{	
	$response["khuvucs"]=array();
	while ($row = $stmt->fetch_assoc())
	{
       	// temp user array
        $kv = array();
        $kv["id"] = $row["id"];
        $kv["tenkhuvuc"] = $row["tenkhuvuc"];  

        //LOAD ALL BANS
        $kvid=$row["id"];

        $query_ban="select id, tenban, trangthai, active from dmban where khuvuc=$kvid"; 
        
        try 
        {        	
        	$stmt_ban=$mysqli->query($query_ban);        	
        	$kv["bans"]=array();

        	while ($ban =$stmt_ban->fetch_assoc()) 
        	{
        		$b=array();
        		$b["id"]=$ban["id"];
        		$b["tenban"]=$ban["tenban"];
        		$b["trangthai"]=$ban["trangthai"];
        		$b["active"]=$ban["active"];        		

        		array_push($kv["bans"], $b);
        	}   
        	$stmt_ban->close(); 
        	
        } 
        catch (Exception $e) 
        {
        	echo "error";
        }        
        // push single product into final response array
        array_push($response["khuvucs"], $kv);
    }    
    $stmt->close();
    echo json_encode($response);
} 
$mysqli->close();
?>